import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {InfoProvider} from "../../providers/InfoProvider";
import {CommProvider} from "../../providers/CommProvider";
import {HttpService} from "../../providers/HttpService";
import {NativeProvider} from "../../providers/NativeProvider";

/**
 * Generated class for the MinePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-mine',
  templateUrl: 'mine.html',
})
export class MinePage {

    items: Array<string> = [];

    commItems: Array<any> = [
        {
            id: 11,
            icon: 'gm-scan',
            title: '扫二维码'
        },
        {
            id: 1,
            icon: 'gm-about',
            title: '关于我们'
        }
    ];

    userItems: Array<any> = [
        {
            id: 2,
            icon: 'gm-avatar',
            title: '成为会员'
        },
    ];

    memberItems: Array<any> = [
        {
            id: 3,
            icon: 'gm-avatar',
            title: '用户信息'
        },
        {
            id: 6,
            icon: 'gm-phone',
            title: '修改手机'
        },
    ];

    adminItems: Array<any> = [
        {
            id: 4,
            icon: 'gm-strategy',
            title: '发布策略'
        },
    ];

    guestItems: Array<any> = [
        {
            id: 5,
            icon: 'gm-avatar',
            title: '用户登录'
        },
    ];

    discoveryItem: Array<any> = [
        {
            id: 10,
            icon: 'gm-share',
            title: '每日分享'
        }
    ];

    user_img: string = 'assets/icon/user.png';
    user_tag: string;
    user_info: any;

    trade_content: Array<string> = [
        '全部价位: ',
        '买入价位: ',
        '卖出价位: ',
        '平仓价位: '
    ];

    pub_trade: any = {
        type: 0,
        price: null,
        content: null,
        user_id: null,
        user_code: null
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private infoProvider: InfoProvider,
                private commProvider: CommProvider,
                private nativeProvider: NativeProvider,
                private httpService: HttpService) {
        this.user_info = this.infoProvider.getUserInfo();

        if (this.user_info.type == 0) {
            this.items = this.userItems.concat(this.discoveryItem);
        } else if (this.user_info.type < 0) {
            this.items = this.guestItems;
        } else if (this.user_info.type == 1) {
            this.user_tag = 'gm-vd';
            this.items = this.memberItems.concat(this.discoveryItem);
        } else if (this.user_info.type > 8) {
            this.user_img = 'assets/icon/admin.png';
            this.user_tag = 'gm-vc';
            this.items = this.adminItems.concat(this.memberItems).concat(this.discoveryItem);
        }
        this.items = this.items.concat(this.commItems);
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad MinePage');
    }

    ionViewWillEnter() {
        this.user_info = this.infoProvider.getUserInfo();
    }

    itemSelected(id) {
        switch (id) {
            case 1:
                this.navCtrl.push('AboutPage', {bizType: 1});
                break;

            case 2:
                this.navCtrl.push('MemberPage');
                break;

            case 3:
                this.navCtrl.push('MemberPage', {
                    edit_type: 1
                });
                break;

            case 4:
                this.navCtrl.push('PubStrategyPage');
                break;

            case 5:
                this.navCtrl.push('LoginPage', {guest_login: true});
                break;

            case 6:
                this.navCtrl.push('ModifyPhonePage');
                break;

            case 10:
                this.navCtrl.push('DiscoveryPage');
                break;

            case 11:
                this.nativeProvider.barCodeScan({
                    preferFrontCamera: false, // iOS and Android
                    showFlipCameraButton: false, // iOS and Android
                    showTorchButton: false, // iOS and Android
                    torchOn: false, // Android, launch with the torch switched on (if available)
                    // prompt: '请将二维码放置在扫描区域内', // Android
                    resultDisplayDuration: 500,
                    formats: 'QR_CODE,PDF_417', // default: all but PDF_417 and RSS_EXPANDED
                    orientation: 'portrait',
                    disableAnimations: true, // iOS
                    disableSuccessBeep: false // iOS
                }).then(result => {
                    if (!result.cancelled) {
                        let dataArray = result.text.split('|');
                        if (dataArray[0] == 'gold') {
                            window.open(dataArray[2]);
                        }
                    }
                }).catch(error => {
                    this.commProvider.showToast(error);
                });
                break;
        }
    }

    onSetting() {
        this.navCtrl.push('SettingPage');
    }

    onPubTradeInfo(tradeType: number) {
        let inputArray = [{
            name: 'price',
            placeholder: '请输入交易价位',
            type: 'number'
        }];
        this.commProvider.showInputAlert('发布交易价位', inputArray).then(resp => {
            console.log(resp);
            this.publicTrade(tradeType, resp.price);
        });
    }

    publicTrade(type: number, price: number) {
        this.pub_trade.type = type;
        this.pub_trade.user_id = this.user_info.id;
        this.pub_trade.user_code = this.user_info.code;
        this.pub_trade.user_phone = this.user_info.phone;
        this.pub_trade.user_name = this.user_info.name;
        this.pub_trade.user_gender = this.user_info.gender;
        this.pub_trade.price = price;
        console.log('onPubTrade:', this.pub_trade);

        if (!this.pub_trade.price) {
            this.commProvider.showToast('缺少交易价位');
            return;
        }

        this.pub_trade.content = this.trade_content[type] + this.pub_trade.price + '元';
        this.commProvider.showConfirmAlert('发布交易信息', this.pub_trade.content)
            .then(confirm => {
                console.log('showConfirmAlert done');
                if (confirm) {
                    this.httpService.instance()
                        .setUrl('/vipTrade/addVipTrade')
                        .setBody(this.pub_trade)
                        .postRequest()
                        .then(res => {
                            if (res.status == 0) {
                                this.commProvider.showToast('发布成功');
                            } else {
                                this.commProvider.showToast(res.msg);
                            }
                        });
                }
            });
    }
}
